package com.iSnail.ms.gather.dao;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Random;

import org.apache.log4j.Logger;

import com.iSnail.ms.gather.db.DBManager;
import com.iSnail.ms.gather.entity.ETemperature;
import com.iSnail.ms.gather.entity.GatherRecord;

public class GatherDataDao {
	private static final Logger log = Logger.getLogger(GatherDataDao.class.getName());
	
	private static final String columns = "gatherid,addr,channel1,channel2,channel3,channel4,channel5,channel6,channel7,channel8,time";
	
	private static String insertsql = "insert gather_data ("+columns+")" + " value(?,?,?,?,?,?,?,?,?,?,now())";
	private static String querysql = "select " + columns + " where id=?";
	
	public boolean insertRecord(int id,float temperature,float strain,float stress,float ausform) throws Exception {
		Connection con = null;
		PreparedStatement psm = null;
		try {
			con = DBManager.getCon();
			psm = DBManager.getPSM(con, insertsql);
			psm.setInt(1, id);
			psm.setFloat(2, temperature);
			psm.setFloat(3, strain);
			psm.setFloat(4, stress);
			psm.setFloat(5, ausform);
			return psm.execute();
		} finally {
			DBManager.closeDbSource(null, psm, con);
		}
	}
	
	public void insertRecordBatch(ArrayList<ETemperature> records) throws Exception {
		Connection con = null;
		PreparedStatement psm = null;
		try {
			con = DBManager.getCon();
			psm = DBManager.getPSM(con, insertsql);
			for(ETemperature each:records){
				psm.setInt(1, each.getGatherid());
				psm.setInt(2, each.getLocal());
				double[] temp = each.getTemperature();
				int len = temp.length;
				for(int i=0;i<len;i++){
					int index = i + 3;
					psm.setDouble(index, temp[i]);
				}
				psm.addBatch();
			}
			psm.executeBatch();
			psm.clearBatch();
		} finally {
			DBManager.closeDbSource(null, psm, con);
		}
	}
	
	public ArrayList<GatherRecord> getRecordByID(int id) throws Exception {
		ArrayList<GatherRecord> result = new ArrayList<GatherRecord>();
		Connection con = null;
		PreparedStatement psm = null;
		ResultSet rs = null;
		GatherRecord record = null;
		try {
			con = DBManager.getCon();
			psm = DBManager.getPSM(con, querysql);
			psm.setInt(1, id);
			rs = psm.executeQuery();
			while (rs.next()) {
				record = new GatherRecord();
//				record.setId(rs.getInt(1));
//				record.setTemperature(rs.getFloat(2));
//				record.setStrain(rs.getFloat(3));
//				record.setStress(rs.getFloat(4));
//				record.setAusform(rs.getFloat(5));
//				record.setTime(rs.getTimestamp(6));
				result.add(record);
			}
			return result;
		} finally {
			DBManager.closeDbSource(null, psm, con);
		}
	}
	
	public static void main(String[] args) throws Exception {
		GatherDataDao dao = new GatherDataDao();
		ArrayList<ETemperature> records = new ArrayList<ETemperature>();
		double[] temp = new double[8];
		for(int i=1;i<889;i++){
			Random random = new Random(System.nanoTime());
			
			ETemperature record = new ETemperature();
			record.setGatherid(678);
			record.setLocal(3);
			for(int j=0;j<8;j++){
				temp[j] = System.currentTimeMillis();
			}
			record.setTemperature(temp);
			record.setTime(System.currentTimeMillis());
			records.add(record);
			//long start = System.currentTimeMillis();
			//dao.insertRecord(random.nextInt(4), random.nextFloat(), random.nextFloat(), random.nextFloat(), random.nextFloat());
			//log.error("used:"+(System.currentTimeMillis()-start)+".ms");
		}
		dao.insertRecordBatch(records);
//		long start = System.currentTimeMillis();
//		dao.getRecordByID(1);
//		log.error("used:"+(System.currentTimeMillis()-start)+".ms");
	}
}